package com.woc.boot.srv.annotation;

import java.util.regex.Pattern;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class IPValidator implements ConstraintValidator<IP, String> {

    private static final Logger LOGGER = LoggerFactory.getLogger(IPValidator.class);
	
	private static final String REGEX = "((2(5[0-5]|[0-4]\\d))|[0-1]?\\d{1,2})(\\.((2(5[0-5]|[0-4]\\d))|[0-1]?\\d{1,2})){3}";

	@Override
	public boolean isValid(String s, ConstraintValidatorContext context) {
		boolean result = false;
		try {
			result = Pattern.matches(REGEX, s);
		} catch (Exception e) {
			LOGGER.error("验证IP格式时发生异常，异常信息：", e);
		}
		return result;
	}
}